package l2capRtpReceiver;

import javax.media.Player;
import javax.swing.JSlider;

public class Slider1Update extends Thread 
{
	private boolean _closed = false;
	private boolean _paused = true;
	
	private Player _player = null;
	private JSlider _jSlider = null;
	private int _offset = 0;

	/**
	 * @return the _offset
	 */
	public int get_offset() {
		return _offset;
	}


	/**
	 * @param _offset the _offset to set
	 */
	public void set_offset(int offset) {
		_offset = offset;
	}


	/**
	 * @return the _closed
	 */
	public boolean is_closed() 
	{
		return _closed;
	}


	/**
	 * @return the _paused
	 */
	public boolean is_paused() 
	{
		return _paused;
	}

	/**
	 * @param _paused the _paused to set
	 */
	public void set_paused(boolean paused) 
	{
		_paused = paused;
		
		synchronized(this)
		{
			notify();
		}
		
	}

	public Slider1Update(Player player, JSlider jSlider)
	{
		_player = player;
		_jSlider = jSlider;
	}
	
	public void run()
	{
		while(_closed == false)
		{

			if(_paused == true)
			{
				synchronized(this)
				{
					while(_paused == true)
					{
						try 
						{
							wait();
						} 
						catch (InterruptedException e) 
						{
							e.printStackTrace();
						}
					}
				}
			}

			if(_closed == false)
			{
				_jSlider.setValueIsAdjusting(true);

				_jSlider.setValue((int)_player.getMediaTime().getSeconds() + _offset);

				_jSlider.setValueIsAdjusting(false);


				try 
				{
					//faccio ripartire il thread tra 2 secondi
					Thread.sleep(2000);
				} 
				catch (InterruptedException e) 
				{
					e.printStackTrace();
				}
			}
		}
	}
	
	public void close() 
	{
		_closed = true;
		
		synchronized(this)
		{
			notify();
		}
	}

}
